package com.bkgy.controller.tpdol3data.controller;

import com.bkgy.controller.accident.service.IAccidentService;
import com.bkgy.controller.acidefficiency.service.AcidEfficiencyService;
import com.bkgy.controller.repOutput.service.IRepOutputService;
import com.bkgy.controller.shutdowndetails.service.ITbDowntimeTcm2280Service;
import com.bkgy.controller.tpdol3data.entity.dto.*;
import com.bkgy.controller.tpdol3data.service.ITPdoL3Tcm2280Service;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Tag(name = "产量系列报表", description = "产量报表统计图")
@Slf4j
@Controller
@RequestMapping("/tpdol3")
@RequiredArgsConstructor
public class TPdoL3Tcm2280Controller {
    private final IRepOutputService repOutputService;
    private final ITPdoL3Tcm2280Service service;
    private final ITbDowntimeTcm2280Service tcmStopService;
    private final AcidEfficiencyService speedService;
    private final IAccidentService accidService;

    @Operation(summary = "作业区总览/作业率")
    @PostMapping("/workRate")
    @ResponseBody
    public ResultData<WorkRate> workRate() {
        return service.workRate();
    }

    @Operation(summary = "作业区总览/月产量统计")
    @PostMapping("/findProdMonthData")
    @ResponseBody
    public ResultData<TpdoL3Res> findL3ProdData() {
        return service.findL3Data();
    }

    @Operation(summary = "作业区总览/月质量统计")
    @PostMapping("/findQuaMonthData")
    @ResponseBody
    public ResultData<TpdoL3Res> findL3QuaData() {
        return service.findL3QuaData();
    }

    @Operation(summary = "作业区总览/三冷区域成材率")
    @PostMapping("/findThrData")
    @ResponseBody
    public ResultData<TpdoL3Res> findL3QuaThrData() {
        return service.findL3SucData();
    }

    @Operation(summary = "作业区总览/生产总览")
    @PostMapping("/findOutputData")
    @ResponseBody
    public ResultData<List<TpdoL3OutputRes>> findOutputData() {
        return repOutputService.findOutputData();
    }

    @Operation(summary = "酸轧班组报表》重点关注列表", description = "重点关注列表")
    @PostMapping("/importantPoint")
    @ResponseBody
    public ResultData importantPoint(@RequestBody OutputReq req) {
        String beginDate = req.getStartDate();
        String endDate = DateUtil.getDateToString(new Date(), "yyyyMMdd");
        if (StringUtils.isEmpty(beginDate)) {
            beginDate = DateUtil.getDateToString(DateUtil.getBeforDate(), "yyyyMMdd");
        } else {
            Date d = DateUtil.getStringToDate("yyyy-MM-dd", beginDate);
            beginDate = DateUtil.getDateToString(d, "yyyyMMdd");
            endDate = DateUtil.getDateToString(DateUtil.getNextDayZero(d), "yyyyMMdd");
        }
        return accidService.importantPoint(beginDate, endDate);
    }

    @Operation(summary = "酸轧班组报表》生产效率分析", description = "柱形图带折线图")
    @PostMapping("/prodEfficiencySteelGrade")
    @ResponseBody
    public ResultData prodEfficiencySteelGrade(@RequestBody OutputReq req) {
        String beginDate = req.getStartDate();
        String endDate = DateUtil.getDateToString(new Date(), "yyyyMMdd");
        if (StringUtils.isEmpty(beginDate)) {
            beginDate = DateUtil.getDateToString(DateUtil.getBeforDate(), "yyyyMMdd");
        } else {
            Date d = DateUtil.getStringToDate("yyyy-MM-dd", beginDate);
            beginDate = DateUtil.getDateToString(d, "yyyyMMdd");
            endDate = DateUtil.getDateToString(DateUtil.getNextDayZero(d), "yyyyMMdd");
        }
        return speedService.prodEfficiencySteelGrade(beginDate, endDate);
    }

    @Operation(summary = "酸轧班组报表》班组停机列表", description = "根据班组查询停机列表")
    @PostMapping("/tcmStop")
    @ResponseBody
    public ResultData tcmStop(@RequestBody OutputReq req) {
        String date = req.getStartDate();
        //默认查询上一天时间
        if (StringUtils.isEmpty(date)) {
            date = DateUtil.getDateToString(DateUtil.getBeforDate(), "yyyy-MM-dd");
        } else {
            Date d = DateUtil.getStringToDate("yyyy-MM-dd", date);
            date = DateUtil.getDateToString(d, "yyyy-MM-dd");
        }
        return tcmStopService.tcmStop(date);
    }

    @Operation(summary = "酸轧班组报表》产量分班组情况", description = "默认查询前一天的数据")
    @PostMapping("/tcmOutputCrew")
    @ResponseBody
    public ResultData tcmOutputCrew(@RequestBody OutputReq tc) {
        if (StringUtils.isNotEmpty(tc.getStartDate())) {
            Date date = DateUtil.getStringToDate("yyyy-MM-dd", tc.getStartDate());
            tc.setStartDate(DateUtil.getDateToString(date, "yyyyMMdd"));
        }
        return service.tcmOutputCrew(tc);
    }

    @Operation(summary = "酸轧班组报表》日产情况", description = "默认查询前一天的数据")
    @PostMapping("/tcmOutput")
    @ResponseBody
    public ResultData tcmOutput(@RequestBody OutputReq tc) {
        if (StringUtils.isNotEmpty(tc.getStartDate())) {
            Date date = DateUtil.getStringToDate("yyyy-MM-dd", tc.getStartDate());
            tc.setStartDate(DateUtil.getDateToString(date, "yyyyMMdd"));
        }
        return service.tcmOutput(tc);
    }

    @Operation(summary = "酸轧机组/产量分析/酸扎机组产量曲线图")
    @PostMapping("/findData")
    @ResponseBody
    public ResultData findData(@RequestBody @Valid TPdoL3TcmBaseRequest req) {
        return service.findData(req.getDateType());
    }

    @Operation(summary = "酸轧机组/产量分析/酸扎机组各班组产量曲线图")
    @PostMapping("/findCrewData")
    @ResponseBody
    public ResultData findCrewData() {
        return service.findCrewData();
    }

    @Operation(summary = "酸轧机组/产量分析/全流程成材率")
    @PostMapping("/findModulesData")
    @ResponseBody
    public ResultData<TpdoL3Res> findModulesData(@RequestBody @Valid TPdoL3TcmRequest req) {
        String type = req.getType();//页签标识
        return service.findModulesData(req);
    }

    @Operation(summary = "酸轧机组/产量分析/非计划停机")
    @PostMapping("/findNonHaltData")
    @ResponseBody
    public ResultData<TcmNoPlanStopRes> findNonHaltData(@RequestBody @Valid TPdoL3TcmBaseRequest l3) {
        return service.findNonHaltData(l3);
    }

    @Operation(summary = "酸轧机组/产量分析/特种规格钢生产情况")
    @PostMapping("/findSpeData")
    @ResponseBody
    public ResultData<TpdoL3Res> findSpeData() {
        return service.findL3Spedata();
    }

    @Operation(summary = "酸轧机组/产量分析/利润")
    @PostMapping("/findProfitData")
    @ResponseBody
    public ResultData<TpdoL3ProfitRes> findProfitData() {
        return service.findL3ProfitData();
    }

    @Operation(summary = "酸轧机组/产量分析可视化/班组产能达成率")
    @PostMapping("/findCrewAchiData")
    @ResponseBody
    public ResultData<TpdoL3Res> findCrewAchiData(@RequestBody @Valid TPdoL3CrewARequest req) {
        return service.findCrewAchiData(req);
    }

    @Operation(summary = "酸轧机组/产量分析可视化/重点品种生产情况")
    @PostMapping("/findImpData")
    @ResponseBody
    public ResultData<TpdoL3ImpRes> findImpData() {
        return service.findL3ImpData();
    }

    @Operation(summary = "酸轧机组/产量分析可视化/酸轧机组月产能利用率")
    @PostMapping("/findUtiData")
    @ResponseBody
    public ResultData<TpdoL3Res> findUtiData() {
        return service.findL3LiyongData();
    }

    @Operation(summary = "【构建特种钢报表数据】")
    @GetMapping("/buildTcm2280SpeRep")
    @ResponseBody
    public ResultData<String> buildTcm2280SpeRep(String startDay, String endDay) {
        try {
            Date d1 = DateUtil.getStringToDate("yyyyMMdd", startDay);
            Date d2 = DateUtil.getStringToDate("yyyyMMdd", endDay);
            if (d1 == null || d2 == null) {
                throw new RuntimeException("时间格式错误");
            }

            while (!d1.after(d2)) {
                service.buildSpecialSteel(d1);
                d1 = DateUtil.getAfterDate(d1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataSuccess(e.getMessage());
        }
        return ResultData.ResultDataSuccess("success");
    }
}
